import {Component, Prop, Vue} from 'vue-property-decorator'
import btnResetPrint from './btn_reset_print.tsx'
import {store} from '@/store/index'
import singlePoptip from './proptip.vue'

@Component({
    components:{
        'l-single-poptip': singlePoptip
    }
})
export default class btnAllocation extends btnResetPrint{
    name:string = 'btn-allocation'

    @Prop()
    conf
    
    get disabled (){
        return this.singleStatus === '分配集印'
    }

    get singleType ():"JQ" | "SY"{
        return this.conf.singleType || "JQ"
    }

    get dataType ():"policys" | "baoKa" | "invoices"{
        return this.conf.dataType || "policys"
    }

    get singles (){
        let formStore = store['carFormStore'];
        let list  = store.getters['carFormStore/currentUsableSingle'][this.dataType];
        let cache = []
        
        list.forEach(ele => {
            cache.push({singleId: ele})
        });

        return cache
        
    }


    render (h){
        let _vue = this;

        return (

            <l-single-poptip 
            onSelect={_vue.select}
            disabled = {_vue.disabled}
            onVisibalSwitch={_vue.getSwitch}
            clickFn={_vue.clickFn} 
            data={_vue.singles} >
            </l-single-poptip>
            
        )
    }

    poptipSwitch (type:boolean){}
 
    getSwitch(fn){
        this.poptipSwitch = fn;
    }

    // 更新单证号（注意赋值形式）
    select(selected){
        store.commit('carFormStore/updateSingleNumber',{serialNumber:selected.singleId} )
    }

    // 请求交险可用单证
    clickFn (){
        let _vue:any = this;
        
        // 当前操作项目
        this.commitCurrentSingle(this.mod.key);

        // 提交请求类型
        store.commit('carFormStore/changeCurrentSingleGetType', _vue.singleType);

        // 是否每次都请求
        store.dispatch('request', {
            url: 'get/usableSingles',
            data: {}
        }).then(success =>{
            
            // 判断是否有单证可用, 无可用单证将请求随机单证号
            if(success && !this.singles.length){
                store.dispatch('request', {
                    url: 'get/single',
                    data: {} 
                })

                setTimeout(() =>{
                    _vue.poptipSwitch(false);
                    _vue.$Notice.warning({
                        title: '单证为随机单证',
                        desc: ` 未获取到保险公司单证号， 当前单证为随机单证号`
                    })
                }, 500)

               
            }
            
        })

    }
    

}